
<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <title>iOS中NSLog打印中文及打印不全的宏</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=2.0">
<link rel="stylesheet", href="/./css/style.css">
<link rel="shortcut icon", href="/./favicon.ico">
<link rel="apple-touch-icon", href="/./apple-touch-icon.png">

        
    
        <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
    
    <link rel='stylesheet' href="/./css/dracula.css">

    <meta name="generator" content="Hexo 4.2.0"></head>
    <body>
        <header class="al_header al_pos_fixed">
    <div class="al_header_container dis_flex_jcenter">
        <div class="al_header_container_left">
            <div class="al_header_site_title">
                <a href="/">Kevin' Blog</a>
            </div>
        </div>

        <div class="dis_flex_jcenter">
            <div class="al_header_setting">
                <svg class="al_header_icon">
                    <use xmlns="http://www.w3.org/2000/svg" xlink:href="/assets/svg_icons.svg#svg-menu"></use>
                </svg>
            </div>
        </div>
    </div>
</header>

        <div class="al_sidebar">

    <div class="al_sidebar_overlay al_full_cover"></div>

    <div class="al_pos_fixed al_sidebar_cnt">
        <div class="dis_flex_acenter al_sidebar_header">
            <h3>Kevin' Blog</h3>
            <div class="al_sidebar_close al_header_setting">
                <svg class="al_header_icon">
                    <use xmlns="http://www.w3.org/2000/svg" xlink:href="/assets/svg_icons.svg#svg-close"></use>
                </svg>
            </div>
        </div>

        <div class="al_sidebar_author_cnt">

            <div class="al_sidebar_author_info">
                <h4>Kevin</h4>
                <img class="al_sidebar_avatar" src="https://gitee.com/zcwtr/zcwtr/raw/master/image/avatar.jpeg">
                <p>極</p>
            </div>

            
        </div>
    </div>
</div>

        
    <div class="dis_flex_center al_lightbox_cnt al_full_cover">
        <img class="al_lightbox_img"/>
    </div>
    <div class="al_page_background dis_flex_center al_full_cover"></div>
    <div class="al_page_container">
        <div class="al_pos_ab al_fake_background"></div>
        <div class="al_main_container al_main_page_container">
            <article class="al_article">
                <header>
                    <h1 class="al_page_title">
                        iOS中NSLog打印中文及打印不全的宏
                    </h1>
                    <div class="al_page_info dis_flex">
                        <div class="al_page_content_info">
                            December 14, 2020 03:12 AM
                        </div>

                        
                            <div class="al_page_content_info">
                                276 words
                            </div>
                        

                        
                            <div class="al_page_content_info">
                                1 minutes read
                            </div>
                        
                        <span class="tags"></span>
                    </div>
                </header>

                

                
                <section id="post-body">
                    <ul>
<li>Xcode打印方法NSLog() 在Xcode8以后就出现了打印输出不完整、打印中文时候显示的问题，调试是在难受。</li>
<li>之前也用过printf替换NSLog，但是还是中文显示不了，于是用分类，就是重新写一个字典和数组的分类，重写他们的- (NSString *)descriptionWithLocale:(id)locale这个方法，搞得很麻烦。现在一个宏搞定，简单拖到pch头文件里就行。</li>
</ul>
<p>##功能：</p>
<ul>
<li>在debug模式打印、release模式下不打印。</li>
<li>解决打印输出不完整，打印中文显示Unicode的问题。</li>
<li>由于里面用了NSString，如果前面没有添加其他头文件里包含，就可能报错，所以在pch文件里需要加：#import &lt;UIKit/UIKit.h&gt;</li>
<li>废话不多说，Here is the code:</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;修复打印不完整，打印中文显示Unicode码问题</span><br><span class="line"></span><br><span class="line">#ifndef __OPTIMIZE__</span><br><span class="line">#define NSLog(FORMAT, ...) fprintf(stderr, &quot;[%s %s %s-%d] %s\n&quot;, __DATE__ , __TIME__, [[[NSString stringWithUTF8String: __FILE__] lastPathComponent] UTF8String], __LINE__, [[[NSString alloc] initWithData:[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] dataUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding] UTF8String]?[[[NSString alloc] initWithData:[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] dataUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding] UTF8String]:[[NSString stringWithFormat: FORMAT, ## __VA_ARGS__] UTF8String]);</span><br><span class="line">#else</span><br><span class="line">#define NSLog(FORMAT, ...) nil</span><br><span class="line">#endif</span><br></pre></td></tr></table></figure>

                </section>

                
                
    <div id="al_gitalk_cnt">
    <script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
    <script>
        var gitalk = new Gitalk({
            clientID: "b311dfc46e6eb58aa382",
            clientSecret: "386b7ecdfdbe4da2b35281a0d168fe13284572cb",
            repo: "comment",
            owner: "ZTKevin",
            admin: "ZTKevin",
            id: "iOS中NSLog打印中文及打印不全的宏",
        });
        gitalk.render("al_gitalk_cnt")
    </script>
                


            
            <nav class="dis_flex post-nav">
                <span class="prev">
                    
                        <a href="/2021/01/04/Runtime/">
                            <span class="arrow">
                                ← 
                            </span>Newer Posts
                        </a>
                    
                </span>
                <span class="next">
                    
                        <a href="/2020/04/22/%E8%81%9A%E7%84%A6%E5%A3%81%E7%BA%B8/">
                            Older Posts
                            <span class="arrow"> → </span>
                        </a>
                    
                </span>
            </nav>
        </div>
    </div>


        <div class="al_index_footer dis_flex_center">
    <div class="al_index_footer_item al_index_footer_title">
        Kevin
    </div>

    
    

    <div class="al_index_footer_item al_index_footer_extra">
        Created By 
        <a href="https://github.com/iGuan7u/Acetolog" target="_blank" rel="noopener">AcetoLog</a>
         · Power By 
        <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo<a>
    </div>

    <div class="al_index_footer_item al_index_footer_extra_right">
        All Right Reserved
    </div>
</div>

        <script type="text/javascript" async="async" src="/javascripts/acelog.js"></script>
        
        
        
        
    </body>
</html>
        